package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.InStockConverter;
import com.goods.business.converter.OutStockConverter;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.mapper.OutStockMapper;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.OutStock;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/10 15:33
 * @Description:
 */
@Service
public class OutStockServiceImpl implements OutStockService {

    @Autowired(required = false)
    private OutStockMapper outStockMapper;

    @Autowired(required = false)
    private ConsumerMapper consumerMapper;

    @Override
    public PageVO<OutStockVO> findOutStockList(Integer pageNum, Integer pageSize, OutStockVO outStockVO) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);

        Integer status = outStockVO.getStatus();
        String outNum = outStockVO.getOutNum();
        Integer type = outStockVO.getType();

        Example o = new Example(OutStock.class);
        Example.Criteria criteria = o.createCriteria();


        criteria.andLike("status", "%" + status + "%");
        if (outNum != null && !"".equals(outNum)) {
            criteria.andEqualTo("outNum", outNum);
        }
        if (type != null) {
            criteria.andLike("type", "%" + type + "%");
        }


        List<OutStock> outStocks = outStockMapper.selectByExample(o);
        List<OutStockVO> outStockVOS = new ArrayList<>();
        //循环查到的列表
        for (OutStock outStock : outStocks) {
            OutStockVO outStockVO1 = OutStockConverter.OutStockProductVO(outStock);

            //通过OutStock表中的consumerId查询consumer表中的应的数据 然赋值
            Long consumerId = outStockVO1.getConsumerId();
            Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);

            outStockVO1.setAddress(consumer.getAddress());
            outStockVO1.setContact(consumer.getContact());
            outStockVO1.setPhone(consumer.getPhone());
            outStockVO1.setName(consumer.getName());
            outStockVOS.add(outStockVO1);
        }
        //转化为分页对象
        PageInfo<OutStock> Info = new PageInfo<>(outStocks);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), outStockVOS);
    }
}
